﻿using System;
using System.Runtime.Serialization;

using FluentAssertions;
using Xunit;

using Unity.String.Exceptions;

namespace Unity.String.Tests.Exceptions
{
    public class NestedStringBuilderCreationExceptionTest
    {
        private const string TestErrorMessage = "Can't build nested string";
        private const string TestParameterName = "testParameterName";
        
        [Fact]
        public void CatchExceptionTest()
        {
            Assert.ThrowsAsync<NestedStringBuilderCreationException>(() =>
                throw new NestedStringBuilderCreationException());
            
            Assert.ThrowsAsync<NestedStringBuilderCreationException>(() =>
                throw new NestedStringBuilderCreationException(TestErrorMessage, new Exception()));

            Assert.ThrowsAsync<NestedStringBuilderCreationException>(() => 
                throw new NestedStringBuilderCreationException(TestErrorMessage, TestParameterName));
        }

        [Fact]
        public void SerializeExceptionTest()
        {
            NestedStringBuilderCreationException testException = 
                new NestedStringBuilderCreationException(TestErrorMessage, TestParameterName);
            testException.Should().BeBinarySerializable();

            SerializationInfo serializationInfo = new SerializationInfo(typeof(NestedStringBuilderCreationExceptionTest),
                new FormatterConverter());
            StreamingContext streamingContext = new StreamingContext();

            testException.GetObjectData(serializationInfo, streamingContext);
        }
    }
}